﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using PccCommonForC;
using System.Text;

public partial class Member_MyOrder_MyAccount : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Ajax.Utility.RegisterTypeForAjax(typeof(Member_MyOrder_MyAccount));
        if (!IsPostBack)
        {
            txtPassword.Text = "";
            BindPlace();
            Get_BirthDay(drlDay, 1, 31);
            Get_BirthDay(drlMonth, 1, 12);
            Get_BirthDay(drlYear, 1930, System.DateTime.Now.Year);
            BindData();
        }
    }



    private void BindData()
    {

        if (Session["UserID"] == null)
            Response.Redirect("~/Member/Login/Login.aspx");
        clsWebShop cls = new clsWebShop();
        DataTable dt = cls.Get_CustomerByUserIDNoneProc(cls.killSQLInjection(Session["UserID"].ToString())).Tables[0];
        if (dt.Rows.Count > 0)
        {
            hidEmail.Text = "aaa";
            txtName.Text = dt.Rows[0]["CusNm"].ToString().Trim();
            txtEmail.Text = dt.Rows[0]["Email"].ToString().Trim();
            txtPhone.Text = dt.Rows[0]["Phone"].ToString().Trim();
            txtAddress.Text = dt.Rows[0]["Address"].ToString().Trim();
            lblTextTiltle.Text = "Thông tin tài khoản: <font color='green'>" + dt.Rows[0]["UserNm"].ToString().Trim()+"</font>";
            ViewState["UserNm"] = dt.Rows[0]["UserNm"].ToString().Trim();
            ViewState["Password"] = dt.Rows[0]["Password"].ToString().Trim();

            if (dt.Rows[0]["PlaceID"].ToString() != "")
            {

                try
                {
                    ddlPlace.SelectedItem.Selected = false;
                    ddlPlace.Items.FindByValue(dt.Rows[0]["PlaceID"].ToString()).Selected = true;
                }
                catch { }
            }
            txtTaxNo.Text = dt.Rows[0]["TaxNo"].ToString().Trim();

            if (dt.Rows[0]["Sex"].ToString() != "")
            {
                try
                {
                    DrlSex.SelectedItem.Selected = false;
                    DrlSex.Items.FindByValue(dt.Rows[0]["Sex"].ToString()).Selected = true;
                }
                catch { }
            }

            if (dt.Rows[0]["BirthDay"].ToString() != "")
            {
                try
                {
                    drlDay.SelectedItem.Selected = false;
                    drlDay.Items.FindByValue(dt.Rows[0]["BirthDay"].ToString().Substring(6, 2)).Selected = true;
                    drlMonth.SelectedItem.Selected = false;
                    drlMonth.Items.FindByValue(dt.Rows[0]["BirthDay"].ToString().Substring(4, 2)).Selected = true;
                    drlYear.SelectedItem.Selected = false;
                    drlYear.Items.FindByValue(dt.Rows[0]["BirthDay"].ToString().Substring(0, 4)).Selected = true;
                }
                catch { }
            }




        }

    }

    private void BindPlace()
    {
        clsWebShop cls = new clsWebShop();
        ddlPlace.DataSource = cls.Get_PlaceNoneProc();
        ddlPlace.DataTextField = "PlaceNm";
        ddlPlace.DataValueField = "PlaceID";
        ddlPlace.DataBind();

    }

    private void Get_BirthDay(DropDownList ddl, int first, int last)
    {

        for (int i = first; i <= last; i++)
        {
            if (i < 10)
                ddl.Items.Add("0" + i);
            else
                ddl.Items.Add(i.ToString());

        }

    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        string strPassEncry = "", strPass = "";
        RijndaelManaged mobjCryptoService = new RijndaelManaged();
        EncrypKey clsEncryp = new EncrypKey(mobjCryptoService);

        if (chkAutoPass.Checked == true)
        {
            strPass = txtPassword.Text;
            strPassEncry = clsEncryp.Encrypting((strPass), System.Configuration.ConfigurationManager.AppSettings["EncrypKey"]);
        }
        else
        {
            strPassEncry = ViewState["Password"].ToString();
        }

        clsWebShop cls = new clsWebShop();
        clsCommon clsCom = new clsCommon();


        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("UserID", Session["UserID"].ToString());
        msg.CreateFirstNode("AutID", "0");
        msg.CreateFirstNode("CusNm", txtName.Text);
        msg.CreateFirstNode("UserNm", ViewState["UserNm"].ToString());
        msg.CreateFirstNode("Password", strPassEncry);
        msg.CreateFirstNode("AccountID", "");
        msg.CreateFirstNode("TaxNo", txtTaxNo.Text);
        msg.CreateFirstNode("Address", txtAddress.Text);
        msg.CreateFirstNode("PlaceID", ddlPlace.SelectedValue);
        msg.CreateFirstNode("BirthDay", drlYear.SelectedItem.Text + drlMonth.SelectedItem.Text + drlDay.SelectedItem.Text);
        msg.CreateFirstNode("Phone", txtPhone.Text);
        msg.CreateFirstNode("Sex", DrlSex.SelectedValue);
        msg.CreateFirstNode("Email", txtEmail.Text);
        msg.CreateFirstNode("Status", "N");
        msg.CreateFirstNode("Rate", "0");
        msg.CreateFirstNode("UpdID", Session["UserID"].ToString());
        string strReturn = cls.Ins_Upd_shr_User(msg.GetXmlStr.ToString());
        msg = new PccMsg(strReturn);
        string strErr = msg.Query("strOutput");
        string strOutUserID = msg.Query("strOutUserID");

        if (strErr == "0" && strOutUserID != "")
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Bảo lưu thành công!!!'); window.location.href='../../Home.aspx'</script>");
        }
    }

    [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)]
    public string AjaxCheckUser(string value)
    {
        string flag = "no";
        clsWebShop cls = new clsWebShop();
        DataTable dt = new DataTable();
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("UserNm", value.Trim());
        msg.CreateFirstNode("ExcUserID", Session["UserID"].ToString());
        dt = cls.Get_shr_UserByUserNm(msg.GetXmlStr.ToString()).Tables[0];
        if (dt.Rows.Count > 0)
            flag = "yes";
        return flag;
    }
    [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)]
    public string AjaxCheckEmail(string value)
    {
        string flag = "no";
        clsWebShop cls = new clsWebShop();
        DataTable dt = new DataTable();
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("ExcUserID", Session["UserID"].ToString());
        msg.CreateFirstNode("Email", value.Trim());
        dt = cls.Get_shr_UserByUserEmail(msg.GetXmlStr.ToString()).Tables[0];
        if (dt.Rows.Count > 0)
            flag = "yes";
        return flag;
    }



}
